package uabc.arduino.componentes;

import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JCheckBox;
import javax.swing.JTextField;

public class TextFieldPendiente extends JTextField implements Observer {
	private int id;
	private int variable;

	public TextFieldPendiente(int id, int variable) {
		this.id = id;
		this.variable = variable;
	}

	@Override
	public void update(Observable o, Object arg) {
		String cadena = (String) arg;
		if (cadena.startsWith("C:EP:")) {
			cadena = cadena.substring(5);
			HashMap<Integer, String[]> valores = decodificar(cadena);
			String[] v = valores.get(id);
			if (v != null) {
				this.setText(v[variable]);
			}
		}
	}

	public HashMap<Integer, String[]> decodificar(String cadena) {
		// 00=1:01=2:02=3:03=4:04=5:05=6 tipo=1
		String[] entradas = cadena.split(":");
		HashMap<Integer, String[]> mapa = new HashMap<Integer, String[]>();
		for (int i = 0; i < entradas.length; i++) {
			String[] entrada = entradas[i].split("=");
			mapa.put(Integer.parseInt(entrada[0]), entrada[1].split("-"));
		}
		return mapa;
	}
}
